#include "project/initialization.h"

void
drawPlyModel(PlyModel* ply_model)
{
	/* Guarda o estado anterior ao desenho */
	glPushMatrix();

	/* Rotacao tem que ser aplicada antes da translacao. */
	glRotatef(
		ply_model->rt_offset->angle,
		ply_model->rt_offset->x_axis,
		ply_model->rt_offset->y_axis,
		ply_model->rt_offset->z_axis
		); 

	glTranslatef(
		-(ply_model->bc_coords->xm), // Centraliza o objeto no eixo x em relacao ao baricentro
		-(ply_model->bc_coords->ym), // Centraliza o objeto no eixo y em relacao ao baricentro
		-(ply_model->bc_coords->zm)  // Centraliza o objeto no eixo z em relacao ao baricentro
	);

	/* Transformacao de translacao no eixo Z atraves da rolagem do botao central do mouse */
	glTranslatef(0.0f, 0.0f, ply_model->tr_offset->z_tr);

	glBegin(GL_POINTS);
		glColor3f(1.0f, 1.0f, 1.0f);
		for (i = 0; i < ply_model->num_faces; i++)
			for (j = 0; j < 3; j++)
				glVertex3f(
					ply_model->vlist[ ply_model->flist[i]->verts[j] ]->x,
					ply_model->vlist[ ply_model->flist[i]->verts[j] ]->y,
					ply_model->vlist[ ply_model->flist[i]->verts[j] ]->z
				);
	glEnd();

	/* Restaura o estado anterior ao desenho */
	glPopMatrix();	
}

void
drawWorldAxis(void)
{
	/* Desenha efetivamente os tres eixos cartesianos */
	glBegin(GL_LINES);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(-100.0f, 0.0f, 0.0f);
		glVertex3f(100.0f, 0.0f, 0.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(0.0f, -100.0f, 0.0f);
		glVertex3f(0.0f, 100.0f, 0.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(0.0f, 0.0f, -100.0f);
		glVertex3f(0.0f, 0.0f, 100.0f);
	glEnd();
}

void
drawScenario(GLfloat size, GLuint slices)
{

	GLfloat x, y, z;
	GLfloat slice_size = size / (float) slices;

	/* * * * * * * * * * * * * * * * * * 
	 * Desenha o plano z = -<size> ... *
	 * * * * * * * * * * * * * * * * * */

	glBegin(GL_LINES);

		// Varredura vertical
		for (x = -size/2.0f; x <= size/2.0f; x += slice_size)
		{
			glVertex3f(x, 0.0f, -size/2.0f);
			glVertex3f(x, size, -size/2.0f);
		}
		
		// Varredura horizontal
		for (y = 0.0f; y <= size; y += slice_size)
		{
			glVertex3f(-size/2.0f, y, -size/2.0f);
			glVertex3f(size/2.0f, y, -size/2.0f);
		}

	glEnd();

	/* * * * * * * * * * * * * * * * * * 
	 * Desenha o plano z = +<size> ... *
	 * * * * * * * * * * * * * * * * * */

	glBegin(GL_LINES);

		// Varredura vertical
		for (x = -size/2.0f; x <= size/2.0f; x += slice_size)
		{
			glVertex3f(x, 0.0f, size/2.0f);
			glVertex3f(x, size, size/2.0f);
		}
		
		// Varredura horizontal
		for (y = 0.0f; y <= size; y += slice_size)
		{
			glVertex3f(-size/2.0f, y, size/2.0f);
			glVertex3f(size/2.0f, y, size/2.0f);
		}

	glEnd();

	/* * * * * * * * * * * * * * * * * * 
	 * Desenha o plano x = -<size> ... *
	 * * * * * * * * * * * * * * * * * */

	glBegin(GL_LINES);

		// Varredura vertical
		for (z = -size/2.0f; z <= size/2.0f; z += slice_size)
		{
			glVertex3f(-size/2.0f, 0.0f, z);
			glVertex3f(-size/2.0f, size, z);
		}
		
		// Varredura horizontal
		for (y = 0.0f; y <= size; y += slice_size)
		{
			glVertex3f(-size/2.0f, y, -size/2.0f);
			glVertex3f(-size/2.0f, y, size/2.0f);
		}

	glEnd();

	/* * * * * * * * * * * * * * * * * * 
	 * Desenha o plano x = +<size> ... *
	 * * * * * * * * * * * * * * * * * */

	glBegin(GL_LINES);

		// Varredura vertical
		for (z = -size/2.0f; z <= size/2.0f; z += slice_size)
		{
			glVertex3f(size/2.0f, 0.0f, z);
			glVertex3f(size/2.0f, size, z);
		}
		
		// Varredura horizontal
		for (y = 0.0f; y <= size; y += slice_size)
		{
			glVertex3f(size/2.0f, y, -size/2.0f);
			glVertex3f(size/2.0f, y, size/2.0f);
		}

	glEnd();

	/* * * * * * * * * * * * * * * * * * 
	 * Desenha o plano y = 0 ...       *
	 * * * * * * * * * * * * * * * * * */

	glBegin(GL_LINES);

		// Varredura vertical
		for (x = -size/2.0f; x <= size/2.0f; x += slice_size)
		{
			glVertex3f(x, 0.0f, size/2.0f);
			glVertex3f(x, 0.0f, -size/2.0f);
		}
		
		// Varredura horizontal
		for (z = -size/2.0f; z <= size/2.0f; z += slice_size)
		{
			glVertex3f(size/2.0f, 0.0f, z);
			glVertex3f(-size/2.0f, 0.0f, z);
		}

	glEnd();

}
